﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmChuyenLopVanHoa : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "lop_hoc";
        private int mintPreLopID = clsConst.gcintNoneValue;

        public frmChuyenLopVanHoa()
        {
            InitializeComponent();
        }

        private void frmChuyenLopNghe_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mKhoaHoc' table. You can move, or remove it, as needed.
            this.mKhoaHocTableAdapter.Fill(this.qLTHDataSet.mKhoaHoc);
            // TODO: This line of code loads data into the 'qLTHDataSet.mLopVanHoaSelectAllWithDetail' table. You can move, or remove it, as needed.
            this.mLopVanHoaSelectAllWithDetailTableAdapter.Fill(this.qLTHDataSet.mLopVanHoaSelectAllWithDetail);

            dgvHsDen.BestFitColumns();
            dgvHsDi.BestFitColumns();
            txtHocSinh.Focus();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!vldCheck.Validate(txtHocSinh)) return;

                string strSearch = "";
                DataTable objData = null;
                strSearch = txtHocSinh.Text.Trim();

                objData = DAO.clsDbAccess.fncSearchHocSinh(Program.gobjDb, strSearch, true);
                grdControlHsDi.DataSource = objData;
                dgvHsDi.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void lokLop_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                int intLopID;

                intLopID = clsCommon.fncCnvNullToInt(lokLop.EditValue);

                if (intLopID != mintPreLopID)
                {
                    mintPreLopID = intLopID;
                    this.tLopVanHoaHocSinhWithDetailTableAdapter.Fill(this.qLTHDataSet.tLopVanHoaHocSinhWithDetail, intLopID);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnChuyen_Click(object sender, EventArgs e)
        {
            try
            {
                int intHsID;
                int intLopID_Di;
                int intLopID_Den;

                bool blnSuccess = true;
                string strHodem, strTen;
                string strLopDi;
                string strLopDen;
                int intKhoas_Di, intKhoas_Den;

                //nếu chưa chọn lớp đến thì thoát
                intLopID_Den = clsCommon.fncCnvNullToInt(lokLop.EditValue);
                if (intLopID_Den <= 0)
                {
                    clsCommon.fncMsgWarning("Cần phải chọn lớp chuyển đến.");
                    return;
                }

                //nếu cùng 1 lớp thì không làm gì
                intLopID_Di = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colLop_ID));
                if (intLopID_Den == intLopID_Di)
                {
                    clsCommon.fncMsgError("Chuyển lớp không thành công. Lớp đi và lớp đến phải khác nhau.");
                    return;
                }

                //nếu khác khóa thì không cho chuyển
                intKhoas_Di = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colKhoas_ID_Di));
                intKhoas_Den = clsCommon.fncCnvNullToInt(lokLopChuyenDen.GetFocusedRowCellValue(colKHS_ID_Den));
                if (intKhoas_Den != intKhoas_Di)
                {
                    clsCommon.fncMsgError("Chuyển lớp không thành công do khác khóa.");
                    return;
                }

                strHodem = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colHoDem));
                strTen = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colTenHS));
                strLopDi = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colLop));
                strLopDen = clsCommon.fncCnvNullToString(lokLopChuyenDen.GetFocusedRowCellValue(colLVH_ten));

                //xác nhận
                if (!clsCommon.fncMsgConfirm(string.Format("Bạn có muốn chuyển học sinh {0} {1} từ lớp {2} sang lớp {3} không?", strHodem, strTen, strLopDi, strLopDen)))
                    return;

                //lấy id học sinh
                intHsID = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colHs_ID));

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    //xóa lớp cũ, thêm lớp mới.
                    blnSuccess = blnSuccess && DAO.TLopVanHoaHocSinhDAO.Delete(Program.gobjDb, Program.gobjTrans, intLopID_Di, intHsID);
                    blnSuccess = blnSuccess && DAO.TLopVanHoaHocSinhDAO.Insert(Program.gobjDb, Program.gobjTrans, new DTO.TLopVanHoaHocSinhDTO(intLopID_Den, intHsID, 0));

                    if (blnSuccess) { Program.gobjTrans.Commit(); }
                    else { Program.gobjTrans.Rollback(); }
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    dgvHsDi.DeleteSelectedRows();
                    dgvHsDen.AddNewRow();
                    dgvHsDen.SetFocusedRowCellValue(colHS_ID_den, intHsID);
                    dgvHsDen.SetFocusedRowCellValue(colLop_ID_HS_den, intLopID_Den);
                    dgvHsDen.SetFocusedRowCellValue(colhs_hodem, strHodem);
                    dgvHsDen.SetFocusedRowCellValue(colhs_ten, strTen);
                    dgvHsDen.UpdateCurrentRow();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}